﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using NetDimension.Weibo;
using SharpICTCLAS;
using System.IO;
using MySql.Data.MySqlClient;
using System.Threading;
using Microsoft.VisualBasic; 

namespace Avenger
{
    public partial class Form1 : Form
    {
        #region const var
        private const string WeiboAccount = @"ufo2243@hotmail.com";
        private const string WeiboPassword = @"seasonlove";
        private const string AppKey = "2023364337";
        private const string AppSecret = "a8255b765b086743a2741162e9e4cadd";
        private const string CallBackUrl = "ufo2243.me";
        #endregion
        private string DictPath = Path.Combine(Environment.CurrentDirectory, "Data") + Path.DirectorySeparatorChar;
        private WordSegment wordSegment;
        private OAuth oauth;
        private long tweetCount = 0;
        public Form1()
        {
            oauth = new OAuth(AppKey, AppSecret, CallBackUrl);
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string text = Microsoft.VisualBasic.Strings.StrConv(SegmentTextBox.Text, VbStrConv.SimplifiedChinese);
            List<WordResult[]> results = segment(text);
            if (results != null && results.Count > 0)
            {
                foreach (WordResult result in results[0])
                {
                    MessageBox.Show(result.sWord);
                }
            }
            
        }
        private List<WordResult[]> segment(string Segment)
        {
            if (wordSegment == null)
            {
                wordSegment = new WordSegment();
                wordSegment.InitWordSegment(DictPath);
            }
            return wordSegment.Segment(Segment);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(downloadTweet), null);
        }
        public void downloadTweet(object state)
        {
            string name = WeiboAccount;
            string password = WeiboPassword;
            tweetListBox.Items.Add("Start connect to database");
            MySqlManager.Instance.connectMySqlServer();
            MySqlManager.Instance.useAvenger();
            tweetListBox.Items.Add("End connect to database");
            tweetListBox.Items.Add("Start connect to weibo");
            oauth.ClientLogin(name, password);
            tweetListBox.Items.Add("End connect to weibo");
            if (oauth.VerifierAccessToken() == TokenResult.Success)
            {
                DateTime time = DateTime.Now.AddHours(8.0d);
                Client sinaWeibo = new Client(oauth);
                while (true)
                {
                    NetDimension.Weibo.Entities.status.Collection statusCollection;
                    try
                    {
                        statusCollection = sinaWeibo.API.Entity.Statuses.PublicTimeline(int.Parse(numberTextBox.Text), 1, false);
                    }
                    catch (Exception e)
                    {
                        tweetListBox.Items.Add("API ERROR");
                        Thread.Sleep(10000);
                        continue;
                    }
                    foreach (NetDimension.Weibo.Entities.status.Entity tweet in statusCollection.Statuses)
                    {
                        if (tweetListBox.Items.Count > 10000)
                            tweetListBox.Items.Clear();
                        try
                        {
                            if (!MySqlManager.Instance.insertTweet(MySqlManager.Instance.praseTweet(tweet)))
                            {
                                continue;
                            }
                        }
                        catch (Exception e)
                        {
                            continue;
                        }
                        ++tweetCount;
                        tweetLabel.Text = tweetCount.ToString();
                    }
                    if (statusCollection.Statuses.ToList().Count != 0)
                        tweetListBox.Items.Add(statusCollection.Statuses.ToList()[0].Text);
                    Thread.Sleep(int.Parse(timeTextBox.Text));
                    timeLabel.Text = (time - DateTime.Now).ToString().Substring(0,9);
                    if (DateTime.Now > time)
                        break;
                }
                MessageBox.Show("Succesful");
            }
        }
        public void connectDataBase()
        {
            MySqlManager.Instance.connectMySqlServer();
            MySqlManager.Instance.buildDataBase();
            MySqlManager.Instance.buildTable();
            
        }

    }
}
